{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 自回归分布滞后模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 原理讲解\n",
    "\n",
    "在自回归模型中，也可以引入其他变量来构成“自回归分布滞后模型”（Autoregressive Distributed Lag Model，简记 $ADL(p,q)$）：\n",
    "\n",
    "$$y_{t}=\\beta_{0}+\\beta_{1} y_{t-1}+\\cdots+\\beta_{p} y_{t-p}+\\gamma_{1} x_{t-1}+\\cdots+\\gamma_{q} x_{t-q}+\\varepsilon_{t}\\tag{1}$$\n",
    "\n",
    "如果自回归分布滞后模型满足以下假定则万事大吉，可以用OLS来估计它。\n",
    "1. $\\mathrm{E}\\left(\\varepsilon_{t} | y_{t-1}, y_{t-2}, \\cdots, x_{1, t-1}, x_{1, t-2}, \\cdots, x_{K, t-1}, x_{K, t-2}, \\cdots\\right)=0$。这个假定类似于严格外生性假设，它意味着扰动项 $\\varepsilon_{t}$ 与所有解释变量的整个历史全部无关。这保证了对滞后期数 $\\left(p, q_{1}, \\cdots, q_{K}\\right)$ 的设定是正确的。如果滞后期数的设定不正确，比如，真实模型还应该包括 $y_{t-（p+1）}$，但该项 $\\beta_{p+1} y_{t-(p+1)}$ 却被纳入扰动项中，则扰动项 $\\varepsilon_{t}$ 便与解释变量相关，导致OLS不致。\n",
    "2. $\\left\\{y_{t}, x_{1 t}, \\cdots, x_{K t}\\right\\}$ 为渐近独立的平稳序列。\n",
    "3. $\\left\\{y_{t}, x_{1 t}, \\cdots, x_{K t}\\right\\}$ 有非零的有限四阶矩。\n",
    "4. 解释变量无完全多重共线性。\n",
    "\n",
    "对滞后期数的选择可以使用信息准则（最小化 AIC 或 BIC），或使用 $t,F$ 检验来检验最后期系数的显著性。更一般地，可以在 $ARMA$ 模型中引入其他变量，称为“$ARMAX$”模型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## matlab 实现\n",
    "\n",
    "ADLxx.m\n",
    "```matlab\n",
    "function [ADLy,ADLx] = ADLxx(Y,X,p,q)\n",
    "\n",
    "%待估计方程：y = c + y(-1) +....+y(-p) + x(-1) + ... + x(-q)\n",
    "% 获取自回归分布滞后模型的估计向量\n",
    "%  X   - 解释变量，列向量\n",
    "%  Y   - 被解释变量，列向量\n",
    "%  p   - ADL模型Y的滞后阶数,标量\n",
    "%  q   - ADL模型X的滞后阶数,标量\n",
    "\n",
    "T = length(Y);\n",
    "N = max(p,q)+1;\n",
    "\n",
    "ADLy = Y(N:T);\n",
    "c = ones(length(ADLy),1);  %常数项\n",
    "ADLx = zeros(length(ADLy),1+p+q);\n",
    "ADLx(:,1) = c;\n",
    "for i=1:p, ADLx(:,1+i) = Y(N-i:T-i);end\n",
    "for i=1:q, ADLx(:,1+p+i) = X(N-i:T-i);end\n",
    "\n",
    "% % 1.载入数据\n",
    "% data = xlsread('C:\\Users\\Administrator\\Desktop\\hourse.xlsx');\n",
    "% f1 = data(:,2); f2 = data(:,3); e = data(:,6);\n",
    "% \n",
    "% % 2.估计ADL模型f1 = c + f1(-1) + f1(-2) + e(-1)\n",
    "% [ADLy,ADLx] = ADLxx(f1,e,2,1);\n",
    "% beta = regress(ADLy,ADLx);\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
